package com.jxw.launcher.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.View;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.jxw.launcher.R;

/* loaded from: classes.dex */
public class JxwBatteryCircular extends View {
    private ValueAnimator animator;
    private int chargingQuantity;
    private int colorBCBCBE;
    private int colorE21E1E;
    private int height;
    private boolean isCharging;
    private boolean isSquare;
    private int lowBatteryThreshold;
    private float mBatteryHeadMargin;
    private Bitmap mChargingBitmap;
    private Paint mChargingBitmapPaint;
    private Path mClickProgressPath;
    private RectF mClipRectF;
    private float mMargin;
    private float mMiddleWidth;
    private Paint mOuterPaint;
    private RectF mOuterRectF;
    private float mOuterRound;
    private float mOuterStrokeWidth;
    private Paint mProgressPaint;
    private float mProgressRadius;
    private RectF mProgressRectF;
    private float mUnitStrokeWidth;
    private int quantity;
    private int width;

    private void initBitmap() {
    }

    public JxwBatteryCircular(Context context) {
        this(context, null);
    }

    public JxwBatteryCircular(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public JxwBatteryCircular(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.colorE21E1E = Color.parseColor("#E21E1E");
        this.mOuterRectF = new RectF();
        this.quantity = 29;
        this.mProgressRectF = new RectF();
        this.mClipRectF = new RectF();
        this.mClickProgressPath = new Path();
        this.isSquare = false;
        this.lowBatteryThreshold = 15;
        initParameter();
        initAllPaints();
        initBitmap();
        initAnimator();
    }

    private void initAnimator() {
        this.animator = ValueAnimator.ofInt(0, 100);
        this.animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { // from class: com.jxw.launcher.view.JxwBatteryCircular.1
            @Override // android.animation.ValueAnimator.AnimatorUpdateListener
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                JxwBatteryCircular.this.chargingQuantity = ((Integer) valueAnimator.getAnimatedValue()).intValue();
                JxwBatteryCircular.this.invalidateView();
            }
        });
        this.animator.setRepeatCount(-1);
        this.animator.setDuration(AdaptiveTrackSelection.DEFAULT_MIN_TIME_BETWEEN_BUFFER_REEVALUTATION_MS);
        this.animator.start();
    }

    private void initParameter() {
        this.colorBCBCBE = 2013265919;
        this.mUnitStrokeWidth = getResources().getDimension(R.dimen.x1);
        this.mOuterStrokeWidth = getResources().getDimension(R.dimen.x1);
        this.mBatteryHeadMargin = getResources().getDimension(R.dimen.x1);
        this.mMargin = getResources().getDimension(R.dimen.x1);
        this.mMiddleWidth = getResources().getDimension(R.dimen.x1);
    }

    private void initAllPaints() {
        this.mOuterPaint = new Paint(1);
        this.mOuterPaint.setDither(true);
        this.mOuterPaint.setAntiAlias(true);
        this.mOuterPaint.setColor(-1);
        this.mProgressPaint = new Paint(1);
        this.mProgressPaint.setDither(true);
        this.mProgressPaint.setAntiAlias(true);
        this.mProgressPaint.setColor(-1);
        this.mProgressPaint.setStrokeWidth(this.mUnitStrokeWidth);
        this.mProgressPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        this.mChargingBitmapPaint = new Paint();
        this.mChargingBitmapPaint.setDither(true);
        this.mChargingBitmapPaint.setAntiAlias(true);
        this.mChargingBitmapPaint.setFilterBitmap(true);
    }

    @Override // android.view.View
    protected void onMeasure(int i, int i2) {
        super.onMeasure(i, i2);
        this.width = getMeasuredWidth();
        this.height = getMeasuredHeight();
        int i3 = this.height;
        this.mOuterRound = i3 / 2.0f;
        float f = this.mMargin;
        this.mProgressRadius = ((i3 - (f * 2.0f)) - (this.mOuterStrokeWidth * 2.0f)) / 2.0f;
        this.mOuterRectF.set(f, f, (this.width - f) - this.mBatteryHeadMargin, i3 - f);
    }

    @Override // android.view.View
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        onOuterDraw(canvas);
        onProgressDraw(canvas);
    }

    private void onOuterDraw(Canvas canvas) {
        this.mOuterPaint.setStrokeWidth(this.mOuterStrokeWidth);
        this.mOuterPaint.setStyle(Paint.Style.STROKE);
        if (this.isSquare) {
            canvas.drawRect(this.mOuterRectF, this.mOuterPaint);
        } else {
            RectF rectF = this.mOuterRectF;
            float f = this.mOuterRound;
            canvas.drawRoundRect(rectF, f, f, this.mOuterPaint);
        }
        this.mOuterPaint.setStrokeWidth(this.mUnitStrokeWidth);
        this.mOuterPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        this.mOuterPaint.setAntiAlias(true);
        Paint paint = new Paint();
        paint.setTypeface(Typeface.create(Typeface.DEFAULT_BOLD, 1));
        paint.setTextSize(getResources().getDimension(R.dimen.x10));
        paint.setColor(-1);
        paint.setAntiAlias(true);
        Rect rect = new Rect();
        paint.getTextBounds(String.valueOf(this.quantity), 0, String.valueOf(this.quantity).length(), rect);
        Paint.FontMetricsInt fontMetricsInt = paint.getFontMetricsInt();
        int measuredWidth = (getMeasuredWidth() / 2) - (rect.width() / 2);
        int measuredHeight = (((getMeasuredHeight() - fontMetricsInt.bottom) + fontMetricsInt.top) / 2) - fontMetricsInt.top;
        canvas.setDrawFilter(new PaintFlagsDrawFilter(0, 3));
        canvas.drawText(String.valueOf(this.quantity), measuredWidth - getResources().getDimension(R.dimen.x1), measuredHeight, paint);
    }

    private void onProgressDraw(Canvas canvas) {
        if (!this.isCharging) {
            if (this.quantity > this.lowBatteryThreshold) {
                this.mProgressPaint.setColor(this.colorBCBCBE);
            } else {
                this.mProgressPaint.setColor(this.colorE21E1E);
            }
        } else {
            this.mProgressPaint.setColor(this.colorBCBCBE);
        }
        float f = this.mMargin;
        float f2 = (((this.width + (f * 3.0f)) - (f * 3.0f)) - (this.mMiddleWidth * 2.0f)) - (this.mOuterStrokeWidth * 4.0f);
        int i = this.isCharging ? this.chargingQuantity : this.quantity;
        float f3 = this.mOuterStrokeWidth;
        float f4 = this.mMiddleWidth;
        float f5 = f3 + 0.0f + f4;
        float f6 = 0.0f + f3 + f4;
        float f7 = (this.mMargin * 3.0f) + f3 + f4 + ((i / 100.0f) * f2);
        float f8 = ((this.height + 0) - f3) - f4;
        this.mProgressRectF.set(f5, f6, f7, f8);
        this.mClipRectF.set(f5, f6, this.mMargin + this.mOuterStrokeWidth + this.mMiddleWidth + f2, f8);
        this.mClickProgressPath.reset();
        Path path = this.mClickProgressPath;
        RectF rectF = this.mClipRectF;
        float f9 = this.mProgressRadius;
        path.addRoundRect(rectF, f9, f9, Path.Direction.CCW);
        if (this.isSquare) {
            canvas.drawRect(this.mProgressRectF, this.mProgressPaint);
            return;
        }
        canvas.clipPath(this.mClickProgressPath);
        RectF rectF2 = this.mProgressRectF;
        float f10 = this.mProgressRadius;
        canvas.drawRoundRect(rectF2, f10, f10, this.mProgressPaint);
    }

    private void onChargingDraw(Canvas canvas) {
        Bitmap bitmap = this.mChargingBitmap;
        canvas.drawBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(), this.mChargingBitmap.getHeight()), new RectF((this.width / 2) - (this.mChargingBitmap.getWidth() / 2), this.mMargin + this.mOuterStrokeWidth + this.mMiddleWidth, (this.width / 2) + (this.mChargingBitmap.getWidth() / 2), ((this.height - this.mMargin) - this.mOuterStrokeWidth) - this.mMiddleWidth), this.mChargingBitmapPaint);
    }

    public void setPower(int i, boolean z) {
        this.quantity = i;
        this.isCharging = z;
        checkQuantity();
        if (this.isCharging) {
            if (this.animator.isRunning() || this.animator.isStarted()) {
                return;
            }
            this.animator.start();
            return;
        }
        if (this.animator.isStarted() || this.animator.isRunning()) {
            this.animator.cancel();
        }
        invalidateView();
    }

    private void checkQuantity() {
        int i = this.quantity;
        if (i < 0) {
            this.quantity = 0;
        } else if (i > 100) {
            this.quantity = 100;
        }
    }

    @Override // android.view.View
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Bitmap bitmap = this.mChargingBitmap;
        if (bitmap == null || bitmap.isRecycled()) {
            return;
        }
        this.mChargingBitmap.recycle();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void invalidateView() {
        if (Looper.getMainLooper() == Looper.myLooper()) {
            invalidate();
        } else {
            postInvalidate();
        }
    }
}
